/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action.Administration;

import ConstantInProject.VariableObject;
import Database.AuthenticationDAO;
import Models.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;


/**
 *
 * @author trang_000
 */
@Namespace("/User")
public class LoginAction extends ActionSupport {

    private String loginName;
    private String password;

    public LoginAction() {
    }

    public String getLoginName() {
        return loginName;
    }

    @Validations(
            requiredStrings = {
        @RequiredStringValidator(type = ValidatorType.FIELD, message = "Login Name is required.")},
            stringLengthFields = {
        @StringLengthFieldValidator(type = ValidatorType.FIELD, message = "Login Name should be from 6 to 30 chracters.", minLength = "6", maxLength = "30")})
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    @Validations(
            requiredStrings = {
        @RequiredStringValidator(type = ValidatorType.FIELD, message = "Password is required.")},
            stringLengthFields = {
        @StringLengthFieldValidator(type = ValidatorType.FIELD, message = "Password should be from 6 to 30 chracters.", minLength = "6", maxLength = "30")})
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    @Action(value = "login", results = {
        @Result(name = "success", location = "/Administration/HomePage.jsp"),
//        @Result(name = "success", location = "/administration/shome.jsp"),
        @Result(name = "input", location = "/Administration/LoginPage.jsp")
    })
    public String execute() throws Exception {
        AuthenticationDAO authenticationDAO = new AuthenticationDAO();

        User user = new User();
        user.setUserName(loginName);
        user.setPassword(password);

        if (authenticationDAO.checkLogin(user)) {
            user = authenticationDAO.getUserFromUsername(loginName); 
            
            Map session = ActionContext.getContext().getSession();
            session.put("user", user);
            VariableObject.currentUser = user;

            addActionMessage("You logged in with these following information:<br>" + user.toString());

            return SUCCESS;
        } else {
            addActionError("Login Failed.");

            return INPUT;
        }
    }

    @Action(value = "logout", results = {
        @Result(name = "success", location = "/redirect/ardrHome.jsp")
    })
    //@SkipValidation
    public String logout() throws Exception {
        AuthenticationDAO staffAuthenticationDAO = new AuthenticationDAO();
        
        Map session = ActionContext.getContext().getSession();
        User staff = (User) session.get("staff"); 
        session.remove("staff");

        addActionMessage("Logged out successfully.");
        return SUCCESS;
    }
}
